package com.ssyu.examination.exam.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.exam.entity.ExamRecords;
import com.ssyu.examination.exam.service.ExamRecordsService;
import com.ssyu.examination.questionbank.entity.QesExamPaperEntity;
import com.ssyu.examination.system.entity.ClazzEntity;

@WebServlet("/ExamRecordsServlet")
public class ExamRecordsServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private static ExamRecordsService recordsService = new ExamRecordsService();

	public ExamRecordsServlet() {
		super();
	}

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("getClazz".equals(command)) {
			getClazz(request, response);
		} else if ("liststatistics".equals(command)) {
			liststatistics(request, response);
		} else if ("addStartStatistics".equals(command)) {
			addStartStatistics(request, response);
		} else if ("stopStartStatistics".equals(command)) {
			stopStartStatistics(request, response);
		} else if ("getTotal".equals(command)) {
			getTotal(request, response);
		}
	}

	/**
	 * 获取班级生成下拉
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void getClazz(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<ClazzEntity> clazzEntities = recordsService.getClazz();
		if (clazzEntities != null) {
			print(response, FastjsonUtil.toJson(clazzEntities));

		}
	}

	/**
	 * 查询考试记录信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void liststatistics(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String userName = request.getParameter("userName");
		String classIdStr = request.getParameter("classId");
		String paperName = request.getParameter("paperName");
		String getPoint1Str = request.getParameter("getPoint1");
		String getPoint2Str = request.getParameter("getPoint2");
		String stateStr = request.getParameter("state");
		int classId = 0;
		int getPoint1 = 0;
		int getPoint2 = 0;
		int state = 0;
		if (StringUtils.isNotBlank(classIdStr)) {
			classId = Integer.parseInt(classIdStr);
		}
		if (StringUtils.isNotBlank(getPoint1Str)) {
			getPoint1 = Integer.parseInt(getPoint1Str);
		}
		if (StringUtils.isNotBlank(getPoint2Str)) {
			getPoint2 = Integer.parseInt(getPoint2Str);
		}
		if (StringUtils.isNotBlank(stateStr)) {
			state = Integer.parseInt(stateStr);
		}
		ExamRecords examRecords = new ExamRecords();
		examRecords.setUser__name(userName);
		examRecords.setClazzId(classId);
		examRecords.setQep__name(paperName);
		examRecords.setGetPoint1(getPoint1);
		examRecords.setGetPoint2(getPoint2);
		examRecords.setIsPass(state);
		PageModel<ExamRecords> pageModel = recordsService.liststatistics(
				examRecords, page, rows);

		if (pageModel != null) {
			print(response, FastjsonUtil.toJsonWithDateFormat(pageModel,
					"yyyy-MM-dd HH:mm:ss"));
		}

	}

	/**
	 * 开始考试
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void addStartStatistics(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int userId = App.getOperateId(request, response);
		int examPlanId = Integer.parseInt(request.getParameter("examPlanId"));
		String startTime = request.getParameter("startTime");

		int row = recordsService.addStartStatistics(userId, examPlanId,
				startTime);

		if (row > 0) {
			print(response, 1);
		}

	}

	/**
	 * 结束考试
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void stopStartStatistics(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int userId = App.getOperateId(request, response);
		int examPlanId = Integer.parseInt(request.getParameter("examPlanId"));
		String submitTime = request.getParameter("submitTime");
		int getPoint = Integer.parseInt(request.getParameter("getPoint"));
		int state = 0;
		QesExamPaperEntity qesExamPaperEntity = recordsService.isPass(getPoint);
		if (qesExamPaperEntity != null) {
			state = 1;
		} else {
			state = 2;
		}
		int row = recordsService.stopStartStatistics(userId, examPlanId,
				submitTime, getPoint, state);

		if (row > 0) {
			print(response, 1);
		}

	}

	/**
	 * 统计考试信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void getTotal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int total = DBUtil.getCount("SELECT COUNT(*) FROM exam_records");
		int pass = DBUtil
				.getCount("SELECT COUNT(*) FROM exam_records WHERE is_pass=1");
		int noPass = DBUtil
				.getCount("SELECT COUNT(*) FROM exam_records WHERE is_pass=2");
		int noSubmit = DBUtil
				.getCount("SELECT COUNT(*) FROM exam_records WHERE is_pass=3");

		double Passrate = (double) pass / total;

		String each = "共" + total + "人,通过" + pass + "人,未通过" + noPass
				+ "人,未提交或超时" + noSubmit + "人,通过率" + Passrate * 100 + "%";
		print(response, each);

	}

}
